using UnityEditor;
using UnityEngine;
using System.Reflection;

[CustomEditor(typeof(MonoBehaviour), true)]
public class MethodButtonDrawer : Editor
{
    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();
        
        // 遍历所有方法
        foreach (var method in target.GetType().GetMethods(
            BindingFlags.Instance | 
            BindingFlags.Public | 
            BindingFlags.NonPublic))
        {
            // 查找MethodButton特性
            var attribute = method.GetCustomAttribute<MethodButtonAttribute>();
            if (attribute == null) continue;
            
            // 生成按钮名称
            var buttonName = string.IsNullOrEmpty(attribute.buttonName) 
                ? method.Name 
                : attribute.buttonName;
            
            // 创建按钮并绑定方法调用
            if (GUILayout.Button(buttonName))
            {
                method.Invoke(target, null);
            }
        }
    }
}